package sz.szsmk.citizencard.ui.dialogs;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.bumptech.glide.Glide;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.ui.activity.LoadingActivity;
import sz.szsmk.citizencard.util.GlideRoundTransform;
import sz.szsmk.citizencard.util.HtmlToFont;
import sz.szsmk.citizencard.util.JumpThirdView;
import sz.szsmk.citizencard.util.SecureSharedPreferences;

import static com.facebook.stetho.inspector.network.PrettyPrinterDisplayType.HTML;

/**
 * Copyright (c)2021/5/26
 * author: hxh
 * created on: 2021/5/26 14:00
 * description:
 */
public class PolicyDialog extends Dialog {
    private Dialog dialog;
    private View view;

    public PolicyDialog(@NonNull Context context) {
        super(context);
    }

    public void showDialog(Activity context){
        dialog = new Dialog(context, R.style.Translucent_NoTitle);
        view = LayoutInflater.from(context).inflate(R.layout.policy_dialog, null);
        ImageView close = view.findViewById(R.id.policy_refuse);
        ImageView agree = view.findViewById(R.id.policy_agree);
        TextView textView = view.findViewById(R.id.policy_text);
        HtmlToFont htmlToFont = new HtmlToFont();

        textView.setText(htmlToFont.getClickableHtml("<font color=\"#999999\">欢迎您使用智慧苏州！我们将通过<font color=\"#2997FB\"><a href= \"https://newapp.szsmk.com/app-front-end/#/common/serviceAgreement\">《智慧苏州服务协议》</a ></font>\n" +
                "    和<font color=\"#2997FB\"><a href= \"http://newapp.szsmk.com/app-front-end/#/common/secretAgreement\">《智慧苏州隐私声明》</a ></font>帮助您了解我们收集、使用、存储和共享个人信息的情况，以及您所享有的相关权利。\n" +
                "        您可通过阅读完整版<font color=\"#2997FB\"><a href= \"https://newapp.szsmk.com/app-front-end/#/common/serviceAgreement\">《智慧苏州服务协议》</a ></font>和<font color=\"#2997FB\"><a href= \"http://newapp.szsmk.com/app-front-end/#/common/secretAgreement\">《智慧苏州隐私声明》</a ></font>了解详细信息。\n" +
                "        如您同意，请点击“同意”开始接受我们的服务</font>",context));

        textView.setMovementMethod(LinkMovementMethod.getInstance());



        CharSequence text = textView.getText();
        if (text instanceof Spannable) {
            int end = text.length();
            Spannable sp = (Spannable) textView.getText();
            URLSpan[] urls = sp.getSpans(0, end, URLSpan.class);
            SpannableStringBuilder style = new SpannableStringBuilder(textView.getText());
            // 全部删除字体的样式也没了
            // style.clearSpans();//should clear old spans
            for (URLSpan url : urls) {
                MyURLSpan myURLSpan = new MyURLSpan(url.getURL());
                // 删除指定的
                style.removeSpan(url);
                style.setSpan(myURLSpan, sp.getSpanStart(url),
                        sp.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            }
            textView.setText(style);
        }




        close.setOnClickListener(v -> {
                dialog.dismiss();
                if(onDisagreeBtnClickListener!=null) {
                    onDisagreeBtnClickListener.onClick();
                }
        });
        agree.setOnClickListener(v -> {
            SecureSharedPreferences.putBoolean("pilicyDialog",false);
            dialog.dismiss();
            if(onAgreeBtnClickListener!=null) {
                onAgreeBtnClickListener.onClick();
            }
        });

        dialog.setCancelable(false);
        dialog.setContentView(view);
        dialog.show();
    }

    public interface OnAgreeBtnClickListener{
        void onClick();
    }

    public interface  OnDisagreeBtnClickListener {
        void onClick();
    }

    private OnAgreeBtnClickListener onAgreeBtnClickListener;

    public void setOnAgreeBtnClickListener(OnAgreeBtnClickListener onAgreeBtnClickListener) {
        this.onAgreeBtnClickListener = onAgreeBtnClickListener;
    }

    private OnDisagreeBtnClickListener onDisagreeBtnClickListener;

    public void setOnDisagreeBtnClickListener(OnDisagreeBtnClickListener onDisagreeBtnClickListener) {
        this.onDisagreeBtnClickListener = onDisagreeBtnClickListener;
    }

    class MyURLSpan extends ClickableSpan {

    private String mUrl;

    MyURLSpan(String url) {
        mUrl = url;
    }

    @Override
    public void onClick(View widget) {
        // TODO Auto-generated method stub

    }
}

}
